package com.accesshollywood.GL;

import static org.lwjgl.opengl.GL11.*;

import org.lwjgl.util.vector.Vector3f;
import org.newdawn.slick.opengl.Texture;

public class Picture extends AnimObject {
	public MasterControl	masterControl;
	private boolean			selected;
	private boolean			prelimCoupleMode;
	private boolean			coupleMode;

	public Keyframe			homeKey;

	public int				idGL;

	public Picture(Texture texture) {
		super();
		selected = false;
		prelimCoupleMode = false;
		coupleMode = false;

		this.texture = texture;
		// System.out.println("Texture loaded: "+texture);
		// System.out.println(">> Texture ID: "+texture.getTextureID());
	}

	public void onClick() {
		if (!prelimCoupleMode) {
			if (selected) {
				deselectMe();
			} else {
				selectMe();
			}
		} else {
			System.out.println("need to de-couple");
			Couple pCouple = (Couple) parent;
			masterControl.moveOutOfCouples(pCouple);

		}
	}

	public boolean isSelected() {
		return selected;
	}

	public void setCoupleMode() {
		coupleMode = true;
		// removeEventListener(MouseEvent.Click, onClick);
		// addEventListener(MouseEvent.Click, onCoupleClick);
	}

	public void setPrelimCoupleMode() {
		prelimCoupleMode = true;
	}

	public void unsetPrelimCoupleMode() {
		prelimCoupleMode = false;
	}

	private void onCoupleClick() {
		Couple pCouple = (Couple) parent;
		Zone pZone = (Zone) pCouple.parent;
		if (pZone.children.size() > 1) {
			pZone.children.remove(pCouple);
			pZone.reLayout(Zone.ANIMATE_RELAYOUT);
		}
	}

	public void selectMe() {
		selected = true;
		if (!parent.children.contains(this)) {
			parent.children.add(this);
		}
		Keyframe newKey = new Keyframe();
		newKey.setPosXYZ(posCurr);
		newKey.setScaleXYZ(new Vector3f(scaleBase * 1.35f, scaleBase * 1.35f, scaleBase * 1.35f));
		newKey.setFrames(25);
		addKeyframe(newKey);
		if (masterControl != null && masterControl.peopleZone != null && masterControl.peopleZone.children != null) {
			if (masterControl.peopleZone.children.size() > 0) {
				masterControl.refreshZoneSelections(masterControl.peopleZone);
			} else {
				masterControl.refreshZoneSelections(masterControl.finalZone);
			}
		}
		processNextKeyframe(false);
	}

	public void deselectMe() {
		selected = false;
		goHome();
		if (masterControl.peopleZone.children.size() > 0) {
			masterControl.refreshZoneSelections(masterControl.peopleZone);
		} else {
			masterControl.refreshZoneSelections(masterControl.finalZone);
		}
	}

	public void unselectMe() {
		selected = false;
	}

	public void goHome() {
		addKeyframe(homeKey);
		processNextKeyframe(false);
	}

	public void shrink() {
		Keyframe newKey = new Keyframe();
		newKey.setPosXYZ(homeKey.getPosXYZ());
		newKey.setScaleXYZ(new Vector3f(scaleBase * .75f, scaleBase * .75f, scaleBase * .75f));
		newKey.setAlpha(1.0f);
		newKey.setFrames(25);
		addKeyframe(newKey);
		processNextKeyframe(false);// POLITE_KEY);
	}

	public void shrinkTiny() {
		Keyframe newKey = new Keyframe();
		newKey.setPosXYZ(homeKey.getPosXYZ());
		newKey.setScaleXYZ(new Vector3f(scaleBase * .015f, scaleBase * .015f, scaleBase * .015f));
		newKey.setAlpha(1.0f);
		newKey.setFrames(25);
		addKeyframe(newKey);
		processNextKeyframe(false);// POLITE_KEY);
	}

	public void resetSize() {
		Keyframe newKey = new Keyframe();
		newKey.setScaleXYZ(new Vector3f(scaleBase, scaleBase, scaleBase));
		newKey.setAlpha(1.0f);
		newKey.setFrames(25);
		addKeyframe(newKey);
		processNextKeyframe(false);// POLITE_KEY);
	}

	public void renderGL() {
		if (texture != null) {
			texture.bind();
		}
		glTranslatef(posCurr.x, posCurr.y, posCurr.z);
		glRotatef(rotCurr.x, 1f, 0f, 0f);
		glRotatef(rotCurr.y, 0f, 1f, 0f);
		glRotatef(rotCurr.z, 0f, 0f, 1f);
		glScalef(scaleCurr.x, scaleCurr.y, scaleCurr.z);
		glTranslatef(-posCurr.x, -posCurr.y, -posCurr.z);

		// glColor3f(1.0f, 0.0f, 1.0f);
		if (((Zone) parent).zoneType == Zone.TYPE.PEOPLE) {
			glBegin(GL_QUADS);
			glTexCoord2f(0, 0);
			glVertex2f(posCurr.x - 512, posCurr.y - 512);
			glTexCoord2f(730 / 1024.0f, 0);
			glVertex2f(posCurr.x + 512, posCurr.y - 512);
			glTexCoord2f(730 / 1024.0f, 730 / 1024.0f);
			glVertex2f(posCurr.x + 512, posCurr.y + 512);
			glTexCoord2f(0, 730 / 1024.0f);
			glVertex2f(posCurr.x - 512, posCurr.y + 512);
			glEnd();
		} else {
			glBegin(GL_QUADS);
			glTexCoord2f(0, 0);
			glVertex2f(posCurr.x - 512, posCurr.y - 256);
			glTexCoord2f(1.0f, 0);
			glVertex2f(posCurr.x + 512, posCurr.y - 256);
			glTexCoord2f(1.0f, 1.0f);
			glVertex2f(posCurr.x + 512, posCurr.y + 256);
			glTexCoord2f(0, 1.0f);
			glVertex2f(posCurr.x - 512, posCurr.y + 256);
			glEnd();
		}

	}

	public float getLeft() {
		return parent.posCurr.x + parent.scaleCurr.x * (posCurr.x - getWidth() / 2.0f);
	}

	public float getTop() {
		return parent.posCurr.y + parent.scaleCurr.y * (posCurr.y + getHeight() / 2.0f);
		// return posCurr.y + getHeight()/2.0f;
	}

	public float getRight() {
		return parent.posCurr.x + parent.scaleCurr.x * (posCurr.x + getWidth() / 2.0f);
		// return posCurr.x + getWidth()/2.0f;
	}

	public float getBottom() {
		return parent.posCurr.y + parent.scaleCurr.y * (posCurr.y - getHeight() / 2.0f);
		// return posCurr.y - getHeight()/2.0f;
	}

	public float getWidth() {
		if (texture != null) {
			if (((Zone) parent).zoneType == Zone.TYPE.PEOPLE) {
				return 1024 * scaleCurr.x;
			} else {
				return 1024 * scaleCurr.x;
			}
		} else {
			return 0;
		}
	}

	public float getHeight() {
		if (texture != null) {
			if (((Zone) parent).zoneType == Zone.TYPE.PEOPLE) {
				return 1024 * scaleCurr.y;
			} else {
				return 512 * scaleCurr.y;
			}
		} else {
			return 0;
		}
	}

}
